/*
// +----------------------------------------------------------------------
// | 南昌速翔网络科技有限公司
// +----------------------------------------------------------------------
// | QQ技术：1424445608   电话/微信：15622289461
// +----------------------------------------------------------------------
// | 请务必尊重知识产权 严格保证不恶意传播产品源码、不得直接对产品本身
// | 进行二次转售或倒卖、不得对产品进行简单包装后声称为自己的产品等。
// | 否则我们有权利收回产品，并根据事态轻重追究相应法律责任。
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2020 http://www.suxiangw.com/ All rights reserved.
// +----------------------------------------------------------------------
*/
// 统一播放视频处理
//  playModule UIPLPlayer  plPlayer video alivcLivePlayer  polyvVideo  txPlayer  tinyPlayer
var _playName = 'alivcLivePlayer';
var url_play = '';
// 初始化
function play_init() {
    //    console.log(api.winName)
    if (_playName == 'playModule') {
        playModule = api.require('playModule');
        playModule.init({
            FullScreenViewIsFont: false,
            isMultiWindow: false,
            // background:'fs://logo.png',
        });
        playModule.addEventListener({
            name: 'bufferingEnd'
        }, function(ret, err) {
            console.log('缓冲结束');

        });
        playModule.addEventListener({
            name: 'bufferingStart'
        }, function(ret, err) {
            console.log('缓冲开始');

        });
        playModule.addEventListener({
            name: 'onPrepared'
        }, function(ret, err) {
            if (ret.status) {
                api.execScript({
                    frameName: _root_name,
                    script: 'video_img()'
                });
            }
        });
    } else if (_playName == 'UIPLPlayer') {
        UIPLPlayer = api.require('UIPLPlayer');
        UIPLPlayer.addEventListener(function(ret, err) {
            console.log("addEventListener" + JSON.stringify(ret));
        });
    } else if (_playName == 'plPlayer') {
        plPlayer = api.require('plPlayer');
        plPlayer.addPlayerStatusListener(function(ret, err) {
            api.bringFrameToFront({
                from: 'index_list_1',
            });
            api.bringFrameToFront({
                from: 'index_list_0',
            });
            api.bringFrameToFront({
                from: 'index_header',
            });
            api.bringFrameToFront({
                from: 'index_footer',
            });
            if (ret) {
                if (ret.eventType == 'ready') {
                    api.execScript({
                        frameName: _root_name,
                        script: 'video_img()'
                    });
                    if (api.systemType == 'android') {
                        plPlayer.play();
                    }
                } else if (ret.eventType == 'error') {
                    _alert("播放失败" + JSON.stringify(ret))
                    _msg(ret + '播放失败，可能视频已删除');
                }
                console.log("监听plPlayer ret:" + JSON.stringify(ret));
            } else {
                console.log("监听plPlayer err:" + JSON.stringify(err));
            }
        });
        api.bringFrameToFront({
            from: 'index_header',
        });
        api.bringFrameToFront({
            from: 'index_footer',
        });
    } else if (_playName == 'video') {
        video = document.getElementById('video');
    } else if (_playName == 'alivcLivePlayer') {
        alivcLivePlayer = api.require('alivcLivePlayer');
        alivcLivePlayer.initPlayer({
            rect: {
                x: 0,
                y: 0,
                w: api.winWidth,
                h: api.winHeight
            },
            fixed: false,
            fixedOn: api.frameName
        }, function(ret) {
            //   alert('初始化播放器initPlayer:' + JSON.stringify(ret));
        })
        alivcLivePlayer.setCirclePlay({
            isCirclePlay: false
        });
        alivcLivePlayer.setVideoScalingMode({
            scalingMode: 'fit'
        });
        // 监听
        alivcLivePlayer.addEventListener(function(ret) {
            addEventListener(ret)
                //      console.log('addEventListener:' + JSON.stringify(ret));
            if (ret.eventType == 'prepared' || ret.eventType == 'frameInfoListener' || ret.eventType == 'frameInfoListener') {
                api.execScript({
                    frameName: _root_name,
                    script: 'video_img()'
                });
                // api.bringFrameToFront({
                //   from: 'index_header',
                // });
                // api.bringFrameToFront({
                //   from: 'index_footer',
                // });
            }

            if (ret.eventType == 'completed' || ret.eventType == 'complete') {
                if (_getPrefs(url_play)) {
                    var url = _getPrefs(url_play);
                    alivcLivePlayer.setCirclePlay({
                        isCirclePlay: true
                    });
                } else {
                    download(url_play);
                    var url = _sx_qiniu + url_play;
                }
                //    console.log(url)
                alivcLivePlayer.prepareToPlay({
                    url: url
                }, function(ret) {
                    //  console.log('prepareToPlay2:' + JSON.stringify(ret));
                    if (ret.status) {
                        if (api.systemType == 'ios') {
                            alivcLivePlayer.play();
                        } else {
                            alivcLivePlayer.prepareAndPlay();
                        }
                    }
                })
            }
        });
        // 监听异常
        alivcLivePlayer.setOnErrorListener(function(ret) {
            console.log('setOnErrorListener错误码：' + JSON.stringify(ret));
        });
    } else if (_playName == 'txPlayer') {
        txPlayer = api.require('txPlayer');
    } else if (_playName == 'tinyPlayer') {

    }
}
// 播放视频
function play(url, type) {
    if (!type) {
        // 判断欢看限制
        if (_is_video()) {
            _suxiangw_msg();
            return;
        }
        // 判断结束
        url_play = url;
        if (_getPrefs(url)) {
            url = _getPrefs(url);
            if (_playName == 'alivcLivePlayer') {
                alivcLivePlayer.setCirclePlay({
                    isCirclePlay: true
                });
            }

        } else {
            if (_playName == 'alivcLivePlayer') {
                download(url);
                alivcLivePlayer.setCirclePlay({
                    isCirclePlay: false
                });
            }
            url = _sx_qiniu + url;
        }
    } else {
        alivcLivePlayer.setCirclePlay({
            isCirclePlay: true
        });
    }
    //  console.log(url)


    if (_playName == 'playModule') {
        // playModule.init({
        // 	FullScreenViewIsFont:false,
        // 	background:'widget://logo.png',
        // });
        playModule.play({
            rect: {
                x: 0,
                y: 0,
                w: api.winWidth,
                h: api.winHeight
            },
            title: '',
            url: url,
            defaultBtn: false,
            isTopView: false,
            isFullBtn: false,
            isShowTimeLable: false,
            isOpenGesture: false,
            isLoop: true,
            isOpenDanmu: false,
            isLocalCache: true,
            fixed: false,
            scalingMode: 3, //	（可选项）视频播放显示铺满模式（1:无缩放, 2:适应大小模式, 3:充满可视范围，可能会被裁剪, 4:缩放到充满视图）
            singlePageModule: true,
        }, function(ret, err) {
            console.log(ret.index);
        });
    } else if (_playName == 'UIPLPlayer') {
        UIPLPlayer.openPlayer({
            rect: {
                x: 0,
                y: 0,
                w: 330,
                h: 560
            },
            videoUrlPath: url,

        }, function(ret, err) {
            if (ret) {
                alert(JSON.stringify(ret));
            } else {
                alert(JSON.stringify(err));
            }
        });
    } else if (_playName == 'plPlayer') {
        plPlayer.openPlayer({
            rect: {
                x: 0,
                y: 0,
                w: api.winWidth,
                h: api.winHeight
            },
            loopPlay: true,
            backgroundPlayEnable: false,
            videoUrlPath: url,
            videoCacheFolderPath: 'wwwsuxiangwcom',
            // videoCacheExtensionName:'wwwsuxiangwcn',
            fixed: false,
            fixedOn: api.frameName
        });
    } else if (_playName == 'video') {
        var html = '<video id="video" src="' + url + '" width="100%" height="' + api.winHeight + 'px" loop="loop" type="video/mp4"  preload="auto" webkit-playsinline="true" playsinline="true" playsinline x5-video-player-type="h5" x5-video-player-fullscreen="portraint" onerror="window.VIDEO_FAILED=1"   poster="' + url + '?vframe/jpg/offset/2/w/' + api.winWidth + '/h/' + api.winHeight + '" autoplay="autoplay"></video>';
        $api.html($api.dom('body'), html);
    } else if (_playName == 'alivcLivePlayer') {
        //    video_moshi(url);
        alivcLivePlayer.prepareToPlay({
            url: url
        }, function(ret) {
            //  console.log('prepareToPlay:' + JSON.stringify(ret));
            if (ret.status) {
                if (api.systemType == 'ios') {
                    alivcLivePlayer.play();
                } else {
                    alivcLivePlayer.prepareAndPlay();
                }
            }
        })

    } else if (_playName == 'txPlayer') {
        var duration
        txPlayer.open({
            url: url,
            title: '',
            pauseInBackground: true,
            frame: {
                x: 0,
                y: 0,
                w: api.winWidth,
                h: api.winHeight
            },
        }, function(ret, err) {
            console.log(JSON.stringify(ret));
            if (ret.duration) {
                duration = ret.duration;
            }
            if (ret.eventType == 'complete') {

            } else {
                if (duration <= (ret.time + 1)) {
                    console.log(duration)
                }
            }


            if (ret.duration >= ret.time) {

            }
            // dmlkZW8uanMNCuWNl+aYjOmAn+e/lOe9kee7nOenkeaKgOaciemZkOWFrOWPuCANClFR5oqA5pyv77yaMTQyNDQ0NTYwOCAgIOeUteivnS/lvq7kv6HvvJoxNTYyMjI4OTQ2MQ0K6K+35Yqh5b+F5bCK6YeN55+l6K+G5Lqn5p2DIOS4peagvOS/neivgeS4jeaBtuaEj+S8oOaSreS6p+WTgea6kOeggeOAgeS4jeW+l+ebtOaOpeWvueS6p+WTgeacrOi6qw0K6L+b6KGM5LqM5qyh6L2s5ZSu5oiW5YCS5Y2W44CB5LiN5b6X5a+55Lqn5ZOB6L+b6KGM566A5Y2V5YyF6KOF5ZCO5aOw56ew5Li66Ieq5bex55qE5Lqn5ZOB562J44CCDQpDb3B5cmlnaHQgKGMpIDIwMTgtMjAxOSBodHRwOi8vd3d3LnN1eGlhbmd3LmNvbS8gQWxsIHJpZ2h0cyByZXNlcnZlZC4=

            // {"eventType":"complete","timeupdate":"true","cmd":"playing","time":11,"vindex":0}
        });
    } else if (_playName == 'tinyPlayer') {
        api.execScript({
            frameName: _root_name,
            script: 'video_img()'
        });
        var tinyPlayer = api.require('tinyPlayer');
        tinyPlayer.showInView({
            videoUrl: url,
            coverUrl: '',
            title: '',
            autoPlay: true,
            rect: {
                x: 0,
                y: 0,
                w: api.winWidth,
                h: api.winHeight
            },

            fixed: false,
            fixedOn: api.frameName
        }, function(ret) {
            console.log("tinyPlayer:" + JSON.stringify(ret));

        });
    }
    // api.bringFrameToFront({
    // 	from: 'index_footer',
    // });
}
// 更换视频地址
function play_url(url) {
    // 判断欢看限制
    if (_is_video()) {
        _suxiangw_msg();
        return;
    }
    // 判断结束

    url_play = url
    if (_getPrefs(url)) {
        url = _getPrefs(url);
        if (_playName == 'alivcLivePlayer') {
            alivcLivePlayer.setCirclePlay({
                isCirclePlay: true
            });
        }
    } else {

        if (_playName == 'alivcLivePlayer') {
            download(url);
            alivcLivePlayer.setCirclePlay({
                isCirclePlay: false
            });
        }
        url = _sx_qiniu + url;
    }
    //  console.log(url)
    pause_start = true;
    if (_playName == 'playModule') {
        playModule.playUrl({
            title: '',
            url: url,
            defaultBtn: false,
            isTopView: false,
            isFullBtn: false,
            isShowTimeLable: false,
            isOpenGesture: false,
            isOpenDanmu: false,
            isLoop: true,
            isLocalCache: true,
            fixed: false,
            scalingMode: 3, //	（可选项）视频播放显示铺满模式（1:无缩放, 2:适应大小模式, 3:充满可视范围，可能会被裁剪, 4:缩放到充满视图）
            singlePageModule: true,
        }, function(ret, err) {
            if (ret.status) {

            }
        })
    } else if (_playName == 'UIPLPlayer') {
        UIPLPlayer.switchPlayUrl({
            url: url,
        })
    } else if (_playName == 'plPlayer') {
        plPlayer.switchPlayUrl({
            url: url,
        }, function(ret, err) {
            console.log('switchPlayUrl:' + JSON.stringify(ret) + JSON.stringify(err));
        });
    } else if (_playName == 'video') {
        var html = '<video id="video" src="' + url + '" width="100%" height="' + api.winHeight + 'px" loop="loop" type="video/mp4"  preload="auto" webkit-playsinline="true" playsinline="true" playsinline x5-video-player-type="h5" x5-video-player-fullscreen="portraint" onerror="window.VIDEO_FAILED=1"   poster="' + url + '?vframe/jpg/offset/2/w/' + api.winWidth + '/h/' + api.winHeight + '" autoplay="autoplay"></video>';
        $api.html($api.dom('body'), html);
        api.execScript({
            frameName: _root_name,
            script: 'video_img()'
        });
    } else if (_playName == 'alivcLivePlayer') {
        //  video_moshi(url);
        alivcLivePlayer.prepareToPlay({
            url: url
        }, function(ret) {
            //  console.log('prepareToPlay2:' + JSON.stringify(ret));
            if (ret.status) {
                if (api.systemType == 'ios') {
                    alivcLivePlayer.play();
                } else {
                    alivcLivePlayer.prepareAndPlay();
                }
            }
        })

    } else if (_playName == 'txPlayer') {
        txPlayer.setPath({
            url: url,
            title: '',
        })
    } else if (_playName == 'tinyPlayer') {
        api.execScript({
            frameName: _root_name,
            script: 'video_img()'
        });
        var tinyPlayer = api.require('tinyPlayer');
        tinyPlayer.showInView({
            videoUrl: url,
            coverUrl: '',
            title: '',
            autoPlay: true,
            rect: {
                x: 0,
                y: 0,
                w: api.winWidth,
                h: api.winHeight
            },

            fixed: false,
            fixedOn: api.frameName
        }, function(ret) {
            console.log("tinyPlayer:" + JSON.stringify(ret));

        });
    }
    // api.execScript({
    // 	frameName: 'index_list',
    // 	script: 'video_img()'
    // });
}

// 暂停视频
function pause() {
    // 暂停视频停止倒计时奖励
    api.execScript({
        frameName: 'time',
        script: 't(' + true + ')'
    });
    if (_playName == 'playModule') {
        playModule.pause();
    } else if (_playName == 'plPlayer') {
        plPlayer.pause();
    } else if (_playName == 'video') {
        video.play();
    } else if (_playName == 'alivcLivePlayer') {
        alivcLivePlayer.pause();
    }
}
// 暂停后播放视频
function start() {
    // 判断欢看限制
    if (_is_video()) {
        _suxiangw_msg();
        return;
    }
    // 判断结束
    api.execScript({
        frameName: 'time',
        script: 't(' + false + ')'
    });
    if (_playName == 'playModule') {
        playModule.start();
    } else if (_playName == 'plPlayer') {
        plPlayer.resume();
    } else if (_playName == 'video') {
        video.pause();
    } else if (_playName == 'alivcLivePlayer') {
        alivcLivePlayer.play();
    }
}

// 停止播放视频
function stop() {
    if (_playName == 'playModule') {
        playModule.stop();
    } else if (_playName == 'plPlayer') {
        plPlayer.stop();
    } else if (_playName == 'video') {
        video.pause();
    } else if (_playName == 'alivcLivePlayer') {
        alivcLivePlayer.pause();
    }
}
// 暂停和开关判断
function x() {
    var frameName = _root_name;
    if (pause_start) {
        // 暂停
        pause();
        pause_start = false;
        var script = 'pause_start();'

    } else {
        var script = 'pause_start(1);'
        pause_start = true;
        start();
    }
    api.execScript({
        frameName: frameName,
        script: script
    });
}
// 统一播放视频结束

// 视频结束
function addEventListener(ret) {
    return;
    var text = '';
    switch (ret.eventType) {
        case 'prepared':
            text = '准备完成';
            break;
        case 'frameInfoListener':
            text = '首帧显示';
            break;
        case 'complete':
            text = '播放完成';
            break;
        case 'seekCompleted':
            text = 'seek完成';
            break;
        case 'stop':
            text = 'stop触发';
            break;
        case 'circleStart':
            text = '循环播放';
            break;
        case 'startCache':
            text = '开始缓冲(ios)';
            break;
        case 'endCache':
            text = '结束缓冲(ios)';
            break;
        case 'frameInfoListener':
            text = '首帧显示时触发';
            break;
        case 'completed':
            text = '视频正常播放完成时触发';
            break;
        case 'stopped':
            text = '使用stop接口时触发';
            break;
        default:
            text = '未知监听代码';
    }
    text = text + " : " + ret.eventType;
    api.execScript({
        name: 'root',
        frameName: 'index_list_1',
        script: '$("#log").prepend("<p>' + text + '</p>");'
    });
    console.log(text)

}
